package com.tiyende.credit.loop.base

import android.app.Activity
import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import androidx.fragment.app.Fragment
import com.lib.common.adapter.CommonBaseAdapterUtil.getColor
import com.tiyende.credit.loop.R
import com.tiyende.credit.loop.databinding.TiyendeParentViewBinding
import com.tiyende.credit.loop.utils.TiyendeTitleBarUtil


/**
 * BaseFragment
 * @Author wwdeng
 * @CreateTime: 2024/11/22 10:32
 * @Annotation：基类 Fragment
 */
abstract class BaseFragment: Fragment(), IBaseView {

    lateinit var rootViewBinding: TiyendeParentViewBinding

    /* 是否拦截返回按钮，默认不拦截直接返回 */
    private var isInterceptBack = false

    private var mActivity: Activity? = null

    override fun onAttach(context: Context) {
        super.onAttach(context)
        mActivity = activity
    }

    override fun getContext(): Context? {
        return mActivity
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        rootViewBinding = TiyendeParentViewBinding.inflate(inflater)
        rootViewBinding.rootTitleBar.setPadding(0, TiyendeTitleBarUtil.getStatusBarHeight(),0,0)
        rootViewBinding.rootContainer.addView(getTiyendeRootView(inflater), FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT))
        return rootViewBinding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        if (savedInstanceState != null){
            return
        }
        setTiyendeTitleBarVisibility(View.GONE)
        //返回
        rootViewBinding.rootBack.setOnClickListener {
            onBackAction()
        }

        initTiyendeView()
        initTiyendeListener()
        loadTiyendeData()
    }

    private var hasLoadedData = false
    override fun onResume() {
        super.onResume()
        if (!hasLoadedData) {
//            loadTiyendeData()
            hasLoadedData = true
        }
    }

    /**
     * 获取布局文件根视图
     *
     * @return
     */
    protected abstract fun getTiyendeRootView(inflater: LayoutInflater): View

    /**
     * 初始化 View 控件
     */
    protected abstract fun initTiyendeView()

    /**
     * 事件监听
     */
    protected abstract fun initTiyendeListener()

    /**
     * 加数据
     */
    protected abstract fun loadTiyendeData()

    /**
     * 设置背景透明
     */
    fun setTiyendeRootParentTransparent(){
        setTiyendeRootParentBackgroundColor(getColor(R.color.transparent))
    }

    /**
     * 设置背景颜色
     */
    private fun setTiyendeRootParentBackgroundColor(color: Int){
        rootViewBinding.rootLayout.setBackgroundColor(color)
    }

    /**
     * 设置背景图片
     */
    fun setTiyendeRootParentBackgroundResource(drawableId: Int){
        rootViewBinding.rootLayout.setBackgroundResource(drawableId)
    }

    /**
     * @功能描述：是否显示Toolbar
     */
    fun setTiyendeTitleBarVisibility(visibility: Int) {
        rootViewBinding.rootTitleBar.visibility = visibility
    }

    /**
     * 设置toolBar背景颜色
     */
    fun setTiyendeTitleBarColor(color: Int) {
        setTiyendeTitleBarVisibility(View.VISIBLE)
        rootViewBinding.rootTitleBar.setBackgroundColor(color)
    }

    /**
     * 设置toolBar背景图片
     */
    fun setTiyendeTitleBarRes(res: Int) {
        setTiyendeTitleBarVisibility(View.VISIBLE)
        rootViewBinding.rootTitleBar.setBackgroundResource(res)
//        rootViewBinding.rootTitleBar.setPadding(0, 0, 0, 0)
    }

    /**
     * 设置是否显示返回
     */
    private fun setTiyendeBackBtnVisibility(visibility: Int) {
        rootViewBinding.rootBack.visibility = visibility
    }

    /**
     * 设置返回按钮图标
     */
    fun setTiyendeBackBtnRes(res: Int){
        setTiyendeBackBtnVisibility(View.VISIBLE)
        rootViewBinding.rootBack.setImageResource(res)
    }

    /**
     * 是否显示Title
     */
    fun setTiyendeTextTitleVisibility(visibility: Int){
        rootViewBinding.rootTitle.visibility = visibility
    }

    /**
     * 设置Title颜色
     */
    fun setTiyendeTextTitleColor(color: Int) {
        setTiyendeTextTitleVisibility(View.VISIBLE)
        rootViewBinding.rootTitle.setTextColor(color)
    }

    /**
     * 设置Title
     */
    fun setTiyendeTextTitle(text: String) {
        setTiyendeTextTitleVisibility(View.VISIBLE)
        rootViewBinding.rootTitle.text = text
    }

    /**
     * 设置Title对齐方式
     * 默认：Gravity.LEFT
     */
    fun setTiyendeTextTitleGravity(gravity: Int){
        setTiyendeTextTitleVisibility(View.VISIBLE)
        rootViewBinding.rootTitle.gravity = gravity
    }

    /**
     * 设置是否显示右侧图标
     */
    private fun setTiyendeRightBtnVisibility(visibility: Int){
        rootViewBinding.rootCustomer.visibility = visibility
    }

    /**
     * 设置右侧按钮图标
     */
    fun setTiyendeRightBtnRes(res: Int){
        setTiyendeRightBtnVisibility(View.VISIBLE)
        rootViewBinding.rootCustomer.setImageResource(res)
    }

    override fun showTiyendeLoading() {
        val act = context as BaseActivity
        act.showTiyendeLoading()
    }

    fun showTiyendeLoading(msg: String="") {
        showTiyendeLoading(isTOuchOut = false, isCancel = true,msg)
    }

    fun showTiyendeLoading(isTOuchOut: Boolean, isCancel: Boolean, msg: String="") {
        val act = context as BaseActivity
        act.showTiyendeLoading(isTOuchOut,isCancel,msg)
    }

    override fun hiddenTiyendeLoading() {
        val act = context as BaseActivity
        act.hiddenTiyendeLoading()
    }

    /**
     * toast
     * @param msg
     */
    override fun showTiyendeToast(msg: String?) {
        val act = context as BaseActivity
        act.showTiyendeToast(msg)
    }

    /**
     * toast
     * @param msg
     */
    fun showTiyendeToastDuration(msg: String?, duration: Int) {
        val act = context as BaseActivity
        act.showTiyendeToastDuration(msg, duration)
    }

    /**
     * 页面跳转（不带参数）
     *
     * @param T
     */
    open fun onIntent(T: Class<*>?) {
        val act = context as BaseActivity
        act.onIntent(T)
    }

    /**
     * 页面跳转（带参数）
     *
     * @param T
     * @param bundle
     */
    open fun onIntent(T: Class<*>?, bundle: Bundle?) {
        val act = context as BaseActivity
        act.onIntent(T,bundle)
    }

    /* 隐藏键盘 */
    fun onHideSoftInput(v: View) {
        val act = context as BaseActivity
        act.onHideSoftInput(v)
    }

    /* 显示键盘 */
    fun onShowSoftInput(v: View) {
        val act = context as BaseActivity
        act.onShowSoftInput(v)
    }

    /* 返回 */
    private fun onBackAction() {
        if (!isInterceptBack) { //不拦截，直接返回
            mActivity?.finish()
        }
    }

    /* 是否拦截返回按钮 */
    fun isInterceptBack(isBackExit: Boolean) {
        isInterceptBack = isBackExit
    }

}
